import 'package:flutter/material.dart';
import 'package:payment/Component/payListView/index.dart';
import 'package:payment/model/PayAndBal.dart';
import 'package:payment/util/DBHelper.dart';
import 'package:payment/util/MoneyUtil.dart';

import 'FormPage.dart';

class HomePage extends StatefulWidget {
  HomePage({Key? key}) : super(key: key);

  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  List<PayAndBal> _payAndBals = [];

  num total = 0;
  num payNum = 0;
  num balNum = 0;

  void getPayAndBal() async {
    DBHelper dbHelper = new DBHelper();
    List<PayAndBal> payAndBals = await dbHelper.getAllPayAndBals();
    payAndBals.sort((a, b) => b.updateTime.compareTo(a.updateTime));
    setState(() {
      if (payAndBals.length != 0) {
        _payAndBals = payAndBals;
        total = payAndBals
            .map((payAndBal) => payAndBal.consumptionNum)
            .reduce((value, element) => value + element);
        balNum = payAndBals
            .map((e) => e.consumptionNum > 0 ? e.consumptionNum : 0)
            .reduce((value, element) => value + element);
        payNum = payAndBals
            .map((e) => e.consumptionNum < 0 ? e.consumptionNum : 0)
            .reduce((value, element) => value + element);
      } else {
        total = 0;
        balNum = 0;
        payNum = 0;
      }
    });
  }

  @override
  void initState() {
    super.initState();
    getPayAndBal();
  }

  AppBar getAppBar(BuildContext context) {
    return AppBar(
      leading: IconButton(
        icon: const Icon(Icons.arrow_back_ios),
        onPressed: () {},
      ),
      title: const Text(
        "收支分析",
        style: TextStyle(fontSize: 18),
      ),
      centerTitle: true,
      elevation: 0,
      actions: <Widget>[
        Image.asset(
          'assets/personService.png',
          width: 25,
          height: 25,
        ),
        IconButton(
          icon: const Icon(Icons.more_horiz),
          onPressed: () {
            Navigator.push(context, MaterialPageRoute<void>(
              builder: (BuildContext context) {
                return FormPage(
                  getPayAndBal: getPayAndBal,
                  total: total,
                );
              },
            ));
          },
        ),
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: getAppBar(context),
      backgroundColor: Theme.of(context).primaryColor,
      body: SafeArea(
        child: Center(
          child: Padding(
            padding: EdgeInsets.symmetric(horizontal: 8),
            child: Column(
              children: <Widget>[
                Padding(
                  padding: EdgeInsets.symmetric(vertical: 3),
                  child: TextField(
                    decoration: InputDecoration(
                      border: OutlineInputBorder(
                        borderRadius: BorderRadius.all(Radius.circular(30.0)),
                      ),
                      hintText: '请输入关键字进行查询',
                      prefixIcon: Container(
                          padding: const EdgeInsetsDirectional.all(10.0),
                          height: 15,
                          width: 15,
                          child: Image.asset('assets/search.png')),
                      isDense: true, // Added this
                      contentPadding: EdgeInsets.all(3),
                      suffixIcon: IconButton(
                        onPressed: () {},
                        icon: Icon(Icons.mic),
                      ),
                    ),
                  ),
                ),
                Padding(
                  padding: EdgeInsets.symmetric(vertical: 3),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      buildButtonRow("本月"),
                      buildButtonRow("分类"),
                      buildButtonRow("借记卡", color: Colors.deepOrange),
                      buildButtonRow("人民币")
                    ],
                  ),
                ),
                Padding(
                  padding: EdgeInsets.symmetric(
                    vertical: 5,
                  ),
                  child: Flex(direction: Axis.horizontal, children: [
                    Expanded(
                      child: Container(
                        height: 90,
                        decoration: BoxDecoration(
                            borderRadius: BorderRadius.circular(20.0),
                            gradient: LinearGradient(
                                //渐变位置
                                begin: Alignment.topLeft,
                                end: Alignment.bottomRight,
                                stops: [
                                  0.0,
                                  1.0
                                ], //[渐变起始点, 渐变结束点]
                                //渐变颜色[始点颜色, 结束颜色]
                                colors: [
                                  Color.fromRGBO(89, 108, 148, 1),
                                  Color.fromRGBO(126, 144, 184, 1)
                                ])),
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceAround,
                          children: [
                            buildPayPadding("支出", payNum.abs().toString()),
                            buildPayPadding("收入", balNum.toString()),
                          ],
                        ),
                      ),
                    ),
                  ]),
                ),
                PayListView(_payAndBals, getPayAndBal),
              ],
            ),
          ),
        ),
      ),
    );
  }

  Column buildPayPadding(String label, String text) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        Text(
          label,
          style: TextStyle(color: Colors.white, fontSize: 16),
        ),
        Text(
          text + " 元",
          style: TextStyle(
              color: Colors.white, fontWeight: FontWeight.bold, fontSize: 16),
        )
      ],
    );
  }

  Row buildButtonRow(String label, {Color color = Colors.black}) {
    return new Row(children: [
      Container(
        child: new Text(
          label,
          style: TextStyle(color: color),
        ),
      ),
      Icon(
        Icons.arrow_drop_down,
        color: Colors.grey,
      ),
    ]);
  }
}
